﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Text;

namespace Catering.Catering_Page.JieGouJianKong
{
    public partial class XiaohaoCharContrast : System.Web.UI.Page
    {
        #region 属性
        List<CheckBox> listCheckBox = null;//存储页面所有CheckBox
        private static string areaId = "";//作业区ID
        private static string classId = "";//班组ID
        private static string[] years;//年份
        private static int[] RawId;//食品大类ID
        FromRecords frRules = new FromRecords();
        View_Monitor vm = new View_Monitor();
        T_RawMaterials rawMaterialsRule = new T_RawMaterials();
        OperationArea OAreaRules = new OperationArea();
        ClassInfo ClassRules = new ClassInfo();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        #region 大类转换种属
        private string EXChangeRAW(string RAWNAME)
        {
            switch (RAWNAME)
            {
                case "肉类":
                    RAWNAME = "禽肉类";
                    break;
                case "水产类":
                    RAWNAME = "鱼虾类";
                    break;
                case "蔬菜类":
                    RAWNAME = "蔬菜类";
                    break;
                case "水果类":
                    RAWNAME = "水果类";
                    break;
                case "粮食类":
                    RAWNAME = "谷类薯类及杂豆";
                    break;
            }
            return RAWNAME;
        }
        #endregion

        protected void lbtnTyleChar_Click(object sender, EventArgs e)
        {
            areaId = UCDDLToolWithYearOrMonthOrDay1.GetDDL_Area();
            RawId = UCSearchToolByZW1.GetRaw();
            years = UCSearchToolByZW1.GetYears();
            classId = UCDDLToolWithYearOrMonthOrDay1.GetDDL_Class();
            DateTime year_current = UCDDLToolWithYearOrMonthOrDay1.GetDateTime(); //当前年月
            string TimeType = UCDDLToolWithYearOrMonthOrDay1.GetTimeType();
            if (year_current == DateTime.MinValue) return;
            switch (TimeType)
            {
                case "Year":
                    if (years != null && years.Count() > 0 && years.Contains(year_current.Year.ToString()))
                    {
                        GetXiaohaoCharByYear(areaId, classId, years, RawId);
                    }
                    else
                    {
                        string[] lastYears = new string[(years == null ? 1 : (years.Count() + 1))];
                        lastYears[0] = year_current.Year.ToString();
                        if (years != null)
                        {
                            for (int i = 0; i < years.Count(); i++)
                            {
                                if (years[i].Equals(year_current.Year.ToString()))
                                {
                                    continue;
                                }
                                lastYears[i + 1] = years[i];
                            }
                        }
                        GetXiaohaoCharByYear(areaId, classId, lastYears, RawId);
                    }
                    
                    break;
                case "Month":
                    GetXiaohaoCharByMonth(areaId, classId, year_current, RawId);
                    break;
                case "Day":
                    GetXiaohaoCharByDay(areaId, classId, year_current, RawId);
                    break;
            }

        }
        private void GetXiaohaoCharByYear(string areaId, string classid, string[] years, int[] RawId)
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            Dictionary<string, Dictionary<string, decimal>> zhonglei = new Dictionary<string, Dictionary<string, decimal>>();
            foreach (string rawName in UCSearchToolByZW1.GetRawName())
            {
                Dictionary<string, decimal> list = GetReportModel(areaId, classId);//班组列表(x轴列表)
                if (!zhonglei.ContainsKey(rawName)) zhonglei.Add(rawName, list);
            }
            if (years != null && years.Count() > 0)
            {
                for (int di = 0; di < years.Count(); di++)
                {
                    if (Convert.ToInt32(years[di]) < 2012)
                    {
                        DataTable dttemp = vm.GetPerson_Avg_ContrastHistoryByYear(pid, areaId, classid, years, RawId);
                        if (dttemp != null && dttemp.Rows.Count > 0)
                        {
                            for (int i = 0; i < dttemp.Rows.Count; i++)
                            {
                                string yeardate = dttemp.Rows[i]["yeardate"].ToString();
                                int tempclassid = Convert.ToInt32(dttemp.Rows[i]["classid"]);
                                string className = dttemp.Rows[i]["cname"].ToString();
                                decimal SumNumber = Convert.ToInt32(dttemp.Rows[i]["SumNumber"]);
                                string typename = EXChangeRAW(dttemp.Rows[i]["typename"].ToString());
                                if (zhonglei.ContainsKey(typename))
                                {
                                    zhonglei[typename][className] = decimal.Round(SumNumber);
                                }
                            }
                            dttemp.Dispose();
                        }
                        break;
                    }
                }
            }
            DataTable dt = vm.GetXiaohao_Contrast(pid, areaId, classid, years, RawId);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string yeardate = dt.Rows[i]["yeardate"].ToString();
                    int tempclassid = Convert.ToInt32(dt.Rows[i]["classid"]);
                    string className = dt.Rows[i]["cname"].ToString();
                    decimal SumNumber = Convert.ToInt32(dt.Rows[i]["SumNumber"]);
                    string typename = EXChangeRAW(dt.Rows[i]["typename"].ToString());
                    if (zhonglei.ContainsKey(typename))
                    {
                        zhonglei[typename][className] = decimal.Round(SumNumber / 1000);
                    }
                }
                dt.Dispose();
            }

            #region 标题

            string Title = "";
            if (years.Count() == 1)
            {
                Title = years[0] + "年";
            }
            else
            {
                for (int i = 0; i < years.Count(); i++)
                {
                    Title += years[i];
                }
                Title += "年";
            }
            if (!string.IsNullOrEmpty(areaId) && !areaId.Equals("-10"))
            {
                Catering_Model.OperationArea AreaInfo = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(areaId));
                Title += AreaInfo.AreaName;
                if (!string.IsNullOrEmpty(classId) && !classId.Equals("-10"))
                {
                    Catering_Model.ClassInfo ClassInfo = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(classId));
                    Title += ClassInfo.CName + "班组";
                }
                else
                {
                    Title += "各班组";
                }
            }
            else
            {
                Title += "所有作业区";
            }
            Title += "消耗分类对比";

            #endregion

            string StrX = "";
            string str = "";
            if (zhonglei != null)
            {
                int n = 0;
                StringBuilder Sresult = new StringBuilder("");
                foreach (string raw in zhonglei.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + raw + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (string key in zhonglei[raw].Keys)
                    {
                        if (n == 0) StrX += "'" + key + "',";
                        StrData += zhonglei[raw][key] + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    if (n == 0) StrX = StrX.Length > 0 ? StrX.Substring(0, StrX.Length - 1) : "";
                    Sresult.Append("},");
                    str = Sresult.ToString();
                    n++;
                }

            }
            StrX = "[" + StrX + "]";
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + StrX + "," + str + ");", true);


        }
        private void GetXiaohaoCharByMonth(string areaId, string classid, DateTime DTIME, int[] RawId)
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetXiaohao_ContrastByMonth(pid,areaId, classid, DTIME, RawId);
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(areaId, classId, DTIME, "消耗分类对比");
            Dictionary<string, Dictionary<string, decimal>> zhonglei = new Dictionary<string, Dictionary<string, decimal>>();

            foreach (string rawName in UCSearchToolByZW1.GetRawName())
            {
                Dictionary<string, decimal> list = GetReportModel(areaId, classId);//班组列表(x轴列表)
                if (!zhonglei.ContainsKey(rawName)) zhonglei.Add(rawName, list);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int tempclassid = Convert.ToInt32(dt.Rows[i]["classid"]);
                    string className = dt.Rows[i]["cname"].ToString();
                    decimal SumNumber = Convert.ToInt32(dt.Rows[i]["SumNumber"]);
                    string typename = dt.Rows[i]["typename"].ToString();
                    if (zhonglei.ContainsKey(typename))
                    {
                        zhonglei[typename][className] = decimal.Round(SumNumber / 1000);
                    }
                }
                dt.Dispose();
            }

            string StrX = "";
            string str = "";
            if (zhonglei != null)
            {
                int n = 0;
                StringBuilder Sresult = new StringBuilder("");
                foreach (string raw in zhonglei.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + raw + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (string key in zhonglei[raw].Keys)
                    {
                        if (n == 0) StrX += "'" + key + "',";
                        StrData += zhonglei[raw][key] + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    if (n == 0) StrX = StrX.Length > 0 ? StrX.Substring(0, StrX.Length - 1) : "";
                    Sresult.Append("},");
                    str = Sresult.ToString();
                    n++;
                }

            }
            StrX = "[" + StrX + "]";
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + StrX + "," + str + ");", true);
        }
        private void GetXiaohaoCharByDay(string areaId, string classid, DateTime DTIME, int[] RawId)
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetXiaohao_ContrastByDay(pid,areaId, classid, DTIME, RawId);
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(areaId, classId, DTIME, "消耗分类对比");
            Dictionary<string, Dictionary<string, decimal>> zhonglei = new Dictionary<string, Dictionary<string, decimal>>();

            foreach (string rawName in UCSearchToolByZW1.GetRawName())
            {
                Dictionary<string, decimal> list = GetReportModel(areaId, classId);//班组列表(x轴列表)
                if (!zhonglei.ContainsKey(rawName)) zhonglei.Add(rawName, list);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int tempclassid = Convert.ToInt32(dt.Rows[i]["classid"]);
                    string className = dt.Rows[i]["cname"].ToString();
                    decimal SumNumber = Convert.ToInt32(dt.Rows[i]["SumNumber"]);
                    string typename = dt.Rows[i]["typename"].ToString();
                    if (zhonglei.ContainsKey(typename))
                    {
                        zhonglei[typename][className] = decimal.Round(SumNumber / 1000);
                    }
                }
                dt.Dispose();
            }
            string StrX = "";
            string str = "";
            if (zhonglei != null)
            {
                int n = 0;
                StringBuilder Sresult = new StringBuilder("");
                foreach (string raw in zhonglei.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + raw + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (string key in zhonglei[raw].Keys)
                    {
                        if (n == 0) StrX += "'" + key + "',";
                        StrData += zhonglei[raw][key] + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    if (n == 0) StrX = StrX.Length > 0 ? StrX.Substring(0, StrX.Length - 1) : "";
                    Sresult.Append("},");
                    str = Sresult.ToString();
                    n++;
                }

            }
            StrX = "[" + StrX + "]";
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + StrX + "," + str + ");", true);
        }

        public Dictionary<int, string> GetClassList(string groupid, string classid)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            if (!string.IsNullOrEmpty(groupid) && !groupid.Equals("-10"))
            {
                if (!string.IsNullOrEmpty(classid) && !classid.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(classid), UCDDLToolWithYearOrMonthOrDay1.GetAllDDL_Class()[Convert.ToInt32(classid)]);
                    return result;
                }
                else
                {
                    return UCDDLToolWithYearOrMonthOrDay1.GetAllDDL_Class();
                }
            }
            else
            {
                Dictionary<int, string> temp = UCDDLToolWithYearOrMonthOrDay1.GetAllDDL_Area();
                List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetAllList(0);
                if (temp.Count > 0)
                {
                    foreach (int id in temp.Keys)
                    {
                        foreach (Catering_Model.ClassInfo item in listClass.Where(c => c.AreaID == id))
                        {
                            result.Add(item.id, item.CName);
                        }
                    }
                }
            }
            return result;
        }

        public Dictionary<string, decimal> GetReportModel(string groupid, string classid)
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            Dictionary<int, string> temp = GetClassList(groupid, classid);
            foreach (string value in temp.Values)
            {
                if (!result.ContainsKey(value)) result.Add(value, 0);
            }
            return result;
        }
    }
}